package com.tadpole.music.view.activity.base;

import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.luck.picture.lib.dialog.CustomDialog;
import com.tadpole.music.R;
import com.tadpole.music.app.MyApplication;
import com.tadpole.music.iView.base.BaseIView;
import com.tadpole.music.utils.Mutils;
import com.tadpole.music.utils.update.AllDialogShowStrategy;
import com.tadpole.music.utils.update.NotificationDownloadCreatorMandatory;
import com.tadpole.music.utils.update.NotificationUpdateCreatorMandatory;
import com.tadpole.music.utils.update.OkhttpCheckWorker;
import com.tadpole.music.utils.update.OkhttpDownloadWorker;
import com.tadpole.music.utils.update.ToastCallback;
import com.tadpole.music.view.widget.LoadingViewDialog;

import org.lzh.framework.updatepluginlib.UpdateBuilder;

/**
 * Created By 孙岩
 * on 2020/8/20
 */
public class BaseActivity extends AppCompatActivity implements BaseIView {
//    进度条
    protected LoadingViewDialog loadingViewDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 初始化进度条 可根据需求做修改
//        loadingViewDialog = new LoadingViewDialog(this, R.style.CustomDialogOne);
        //设置app顶部状态栏颜色
        Mutils.changeTitleColor(getWindow());

        MyApplication.getInstance().finishManager.addActivity(this);

        String mCurVersionName = Mutils.getLocalVersionName(this);
        int mCurVersionCode = Mutils.getLocalVersion(this);
        ToastCallback callback = new ToastCallback(this, new ToastCallback.ToastCallbackI() {
            @Override
            public void Failure(int type, String msg) {

            }

            @Override
            public void succeed(int type, String msg) {

            }
        });
        UpdateBuilder builder = UpdateBuilder.create();
        // 配置toast通知的回调
        builder.setDownloadCallback(callback);
        builder.setCheckCallback(callback);

        // 根据各项是否选择使用默认配置进行使用更新。
        builder.setCheckWorker(OkhttpCheckWorker.class);
        builder.setUpdateStrategy(new AllDialogShowStrategy());
        builder.setDownloadNotifier(new NotificationDownloadCreatorMandatory());
        builder.setCheckNotifier(new NotificationUpdateCreatorMandatory(builder, mCurVersionName, mCurVersionCode, new NotificationUpdateCreatorMandatory.NotificationUpdateCreatorI() {
            @Override
            public void onClick(int button) {
                if (button == 0) {

                }
            }

            @Override
            public void isNotMandatory() {//不是强制升级

            }
        }));
        builder.setDownloadWorker(OkhttpDownloadWorker.class);
        builder.check();
    }

    @Override
    public void showLoading() {
//        if (!loadingViewDialog.isShowing()) {
//            loadingViewDialog.show();
//        }
    }

    @Override
    public void hideLoading() {
//        if (loadingViewDialog.isShowing()) {
//            loadingViewDialog.dismiss();
//        }
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showErr() {
        Toast.makeText(this, "网络请求失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public Context getContext() {
        return BaseActivity.this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().finishManager.removeActivity(this);
//        customDialog.dismiss();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }
}
